package com.love.poetry.infrastructure.util;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.codehaus.jackson.map.ObjectMapper;

public class ToolUtils {
		
	public static String uniqueID(){
				
		Long uniqueID = System.currentTimeMillis();
		uniqueID++;
		return String.valueOf(uniqueID * 100L + (long)(new Random()).nextInt(9999));
	}
	
	/**
	 * 判断对象是否为空，对象可为字符串、对象数组、Collection。对于字符串会先去除前后的空字符。
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj){
		if (obj == null){
			return true;
		}
		if (obj instanceof String){
			return ((String) obj).trim().equals("");
		}
		if (obj instanceof Object[]){
			return ((Object[]) obj).length == 0;
		}
		if (obj instanceof Collection){
			return ((Collection<?>) obj).isEmpty();
		}
		return false;
	}
	
	/**
	 * 判断对象是否为非空，对象可为字符串、对象数组、Collection。对于字符串会先去除前后的空字符。
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object obj){
		return !ToolUtils.isEmpty(obj);
	}
	
	/**
	 * md5加密
	 */
	public static String md5(String src){
		if (src == null)
			return null;
		try{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(src.getBytes());
			StringBuilder dist = new StringBuilder();
			for (byte b : md.digest()){
				String s = Integer.toHexString(0xFF & b);
				if (s.length() == 1) dist.append("0");
				dist.append(s);
			}
			return dist.toString().toUpperCase();
		}
		catch (Exception e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 去掉前后空白字符，包括中文空格
	 */
	public static String trimCn(String str){
		if (str == null)
			return null;
		str = str.trim();
		if (str.length() == 0)
			return str;
		int st = 0;
		int len = str.length();
		while((st < len) && str.charAt(st) == '　')
			st ++;
		while((st < len) && str.charAt(len - 1) == '　')
			len --;
		if (st > 0 || len < str.length())
			str = str.substring(st, len);
		return str.trim();
	}
	
	public static boolean contains(Object[] array, Object item){
		for (Object obj : array){
			if (obj.equals(item)){
				return true;
			}
		}
		return false;
	}
	
	public static String isoToUtf8(String str){
		if (str == null) return null;
		try{
			return new String(str.getBytes("iso_8859_1"), "utf-8");
		}
		catch (Exception e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 将对象转换成json字符串。如果只有一个参数则直接转换，否则参数个数必须为偶数，并以奇数参数为属性以偶数参数为值转换。
	 */
	public static String toJson(Object... args){
		try{
			ObjectMapper mapper = new ObjectMapper();
			if (args.length == 0){
				return "null";
			}
			else if (args.length == 1){
				return mapper.writeValueAsString(args[0]);
			}
			if (args.length % 2 != 0){
				throw new RuntimeException("调用BookUtils.toJson()方法参数个数必须为偶数");
			}
			Map<String, Object> map = new HashMap<String, Object>(args.length / 2);
			for (int i = 0; i < args.length; i += 2){
				map.put(args[i].toString(), args[i + 1]);
			}
			return mapper.writeValueAsString(map);
		}
		catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static List<?> jsonToList(String json){
		try{
			ObjectMapper mapper = new ObjectMapper();
			return (List<?>)mapper.readValue(json, List.class);
		}
		catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, ?> jsonToMap(String json){
		try{
			ObjectMapper mapper = new ObjectMapper();
			return (Map<String, ?>)mapper.readValue(json, Map.class);
		}
		catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 将日期转换成字符串，格式为yyyy-MM-dd HH:mm:ss
	 */
	public static String formatDate(Date date){
		if (date == null) return null;
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}
	
	/**
	 * 将日期字符串转换成日期，只支持如下格式：
	 * yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
	 */
	public static Date parseDate(String str){
		if (str == null) return null;
		try{
			String f = (str.length() == 10 ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss");
			return new SimpleDateFormat(f).parse(str);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将字符串转换为Long，转换失败返回null
	 */
	public static Long parseLong(String str){
		return ToolUtils.parseLong(str, null);
	}
	
	/**
	 * 将字符串转换为Long，转换失败返回defaultValue
	 */
	public static Long parseLong(String str, Long defaultValue){
		if (str == null) return null;
		try{
			return Long.parseLong(str);
		}
		catch(Exception e){
			
		}
		return defaultValue;
	}
	
}
